//
//  HLMessageSearchUserViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/6/16.
//

#import "HLMessageSearchUserViewController.h"
#import "HLUserDetailViewController.h"
#import "HLUserListViewController.h"
#import "HLSearchUserCell.h"
#import "HLSearchUserRequest.h"
@interface HLMessageSearchUserViewController ()
@property (nonatomic, strong) QMUIButton *backButton;
@property (nonatomic, strong) QMUITextField *searchBar;
@property (nonatomic, strong) QMUIButton *searchButton;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) QMUIFloatLayoutView *labelsView;
@property (nonatomic, strong) QMUIButton *taggleButton;
@property (nonatomic, assign) BOOL isMore;
@end

@implementation HLMessageSearchUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.request start];
}
- (void)initTableView
{
    [super initTableView];
    self.isMore = YES;
    [self.tableView registerClass:[HLSearchUserCell class] forCellReuseIdentifier:[HLSearchUserCell identifyCell]];
    self.tableView.tableHeaderView = [self headerView];
}

- (UIView *)headerView
{
    CGFloat headerHeight = 218;
    if (!self.isMore) {
        headerHeight = 136;
    }
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, headerHeight)];
    header.clipsToBounds = YES;
    self.backButton = [[QMUIButton alloc] init];
    [_backButton setImage:UIImageMake(@"nav_back_black") forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *searchLeftView = [[UIView alloc] init];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:UIImageMake(@"search_user_icon")];
    [searchLeftView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.right.mas_equalTo(-2);
    }];
    
    if (!self.searchBar) {
        self.searchBar = [[QMUITextField alloc] init];
        _searchBar.backgroundColor = HLBackgroundColor;
        _searchBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString: @"请输入用户ID" attributes:@{NSFontAttributeName: UIFontMake(12), NSForegroundColorAttributeName:HLTextColor2}];
        _searchBar.leftView = searchLeftView;
        _searchBar.leftViewMode = UITextFieldViewModeAlways;
        _searchBar.layer.masksToBounds = YES;
        _searchBar.font = UIFontMake(14);
        _searchBar.textColor = HLTextColor1;
        _searchBar.layer.cornerRadius = 17;
        _searchBar.keyboardType = UIKeyboardTypeNumberPad;
        [_searchBar becomeFirstResponder];
    }
    
    [header addSubview:_searchBar];
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-52);
        make.left.mas_equalTo(48);
        make.height.mas_equalTo(34);
        make.top.mas_equalTo(0);
    }];
    
    [header addSubview:_backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(40);
        make.centerY.equalTo(_searchBar);
        make.left.mas_equalTo(4);
    }];
    
    self.searchButton = [[QMUIButton alloc] init];
    [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [_searchButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [_searchButton.titleLabel setFont:UIFontMake(14)];
    [_searchButton addTarget:self action:@selector(searchRequest) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:_searchButton];
    [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_searchBar);
        make.right.mas_equalTo(-12);
    }];
    
    self.labelTitle = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(16) textColor:HLTextColor1];
    _labelTitle.text = @"热门搜索";
    [header addSubview:_labelTitle];
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.equalTo(_searchBar.mas_bottom).offset(16);
    }];
    
    self.labelsView = [[QMUIFloatLayoutView alloc] init];
    _labelsView.padding = UIEdgeInsetsMake(3, 3, 3, 3);
    _labelsView.itemMargins = UIEdgeInsetsMake(3, 3, 3, 3);
    _labelsView.minimumItemSize = CGSizeMake(48, 26);// 以2个字的按钮作为最小宽度
    [header addSubview:_labelsView];
    [_labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_labelTitle.mas_bottom).offset(16);
        make.left.mas_equalTo(6);
        make.right.mas_equalTo(-6);
        make.bottom.mas_equalTo(0);
    }];
    [self updateLabels];
    
    self.taggleButton = [[QMUIButton alloc] init];
    if (self.isMore) {
        [_taggleButton setImage:UIImageMake(@"user_search_up") forState:UIControlStateNormal];
    } else {
        [_taggleButton setImage:UIImageMake(@"user_search_down") forState:UIControlStateNormal];
    }
    [_taggleButton addTarget:self action:@selector(toggleAction:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:_taggleButton];
    [_taggleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_labelsView).offset(-8);
        make.right.equalTo(_labelsView).offset(-8);
    }];
    
    return header;
}

-(void)updateLabels
{
   NSInteger tagIndex = 0;
   for (HLHomeLabelModel *label in [HLRunTime shareInstance].labels) {
       QMUIButton *labelButton = [[QMUIButton alloc] init];
       [labelButton setTitle:label.tagName forState:UIControlStateNormal];
       [labelButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
       [labelButton.titleLabel setFont:UIFontMediumMake(12)];
       labelButton.layer.masksToBounds = YES;
       labelButton.layer.cornerRadius = 12;
       labelButton.backgroundColor = HLBackgroundColor;
       labelButton.contentEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
       labelButton.tag = tagIndex;
       [labelButton addTarget:self action:@selector(labelAction:) forControlEvents:UIControlEventTouchUpInside];
       if (![label.tagGroup isEqualToString:@"local"]) {
           if ((tagIndex < 9 && !self.isMore) || self.isMore){
               [_labelsView addSubview:labelButton];
           }
       }
       
       tagIndex += 1;
   }
}

- (void)toggleAction:(QMUIButton *)toggleButton
{
    self.isMore = !self.isMore;
    self.tableView.tableHeaderView = [self headerView];
    [self.tableView reloadData];
}

- (void)labelAction:(QMUIButton *)labelButton
{
    HLHomeLabelModel *label = [[HLRunTime shareInstance].labels objectAtIndex:labelButton.tag];
    HLUserListViewController *userListVC = [[HLUserListViewController alloc] initWithLabel:label];
    [self.navigationController pushViewController:userListVC animated:YES];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasources.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLSearchUserCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLSearchUserCell identifyCell]];
    if (self.datasources.count <= indexPath.row){
        return cell;
    }
    cell.model = self.datasources[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLSearchUserModel *record = self.datasources[indexPath.row];
    HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:record.userId];
    [self.navigationController pushViewController:userDetailVC animated:YES];
}



- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    
    HLSearchUserRequest *searchRequest = (HLSearchUserRequest *)request;
    HLSearchUserModel *user = searchRequest.userList;
    if (user){
        [self.datasources addObject:user];
    }
    
    if (user == nil){
        [self.emptyView setLoadingViewHidden:YES];
        [self.emptyView setImage:UIImageMake(@"empty_icon_7")];
        [self.emptyView setTextLabelText:@"被你难倒了，换个词试试吧～"];
    } else {
        [self hideEmptyView];
    }
    
    [self.tableView reloadData];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [super requestFailed:request];
}

- (BOOL)preferredNavigationBarHidden
{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}



- (void)searchRequest
{
    NSString *userID = _searchBar.text;
    
    if (userID.length == 0){
        [QMUITips showWithText:@"请输入用户ID"];
        return;
    }
    [self.datasources removeAllObjects];
    
    self.request = [[HLSearchUserRequest alloc] initWithKeyword:userID];
    self.request.delegate = self;
    [self requestInit];
}

@end
